//
//  GHVerifyViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHSetPwdViewController.h"
#import "UITextField+STInputLimit.h"
@interface GHSetPwdViewController ()
@property(nonatomic, strong) UITextField                     *oldTextFiled;
@property(nonatomic, strong) UITextField                     *pwdTextFiled;
@property(nonatomic, strong) UITextField                     *confimpwdTextFiled;
@end

@implementation GHSetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    [self configSubView];
    [self addNotifacations];
    // Do any additional setup after loading the view.
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)UITextFieldTextDidChange:(NSNotification*)notify{
    if (notify.object == self.pwdTextFiled) {
        [self.pwdTextFiled st_textInputLimitWithLength:16];
    }
    if (notify.object == self.confimpwdTextFiled) {
        [self.confimpwdTextFiled st_textInputLimitWithLength:16];
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    [self configTableHeader];
    [self configTableFooter];
}
- (void)configTableHeader{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor =  self.tableView.backgroundColor;
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 100)];
    whiteView.backgroundColor =  [UIColor whiteColor];
    [header addSubview:whiteView];
    
    self.oldTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, UIScreenWidth - 20, 44)];
    self.oldTextFiled.placeholder = @"请输入原密码";
    self.oldTextFiled.keyboardType = UIReturnKeyDefault;
    self.oldTextFiled.textColor= GH_secendTextColor;
    self.oldTextFiled.secureTextEntry = YES;
    self.oldTextFiled.clearButtonMode = UITextFieldViewModeAlways;
    [whiteView addSubview:self.oldTextFiled];
    
    UIView * line0 = [[UIView alloc] initWithFrame:CGRectMake(10, self.oldTextFiled.st_bottom+ 5, UIScreenWidth - 20, 1)];
    line0.backgroundColor = GH_lineColor;
    [whiteView addSubview:line0];
    
    
    self.pwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 10 + line0.st_bottom, UIScreenWidth - 20, 44)];
    self.pwdTextFiled.placeholder = @"新密码(6-16个字符,数字，字母和符号)";
    self.pwdTextFiled.keyboardType = UIReturnKeyDefault;
    self.pwdTextFiled.textColor= GH_secendTextColor;
    self.pwdTextFiled.secureTextEntry = YES;
    self.pwdTextFiled.clearButtonMode = UITextFieldViewModeAlways;
    [whiteView addSubview:self.pwdTextFiled];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(10, self.pwdTextFiled.st_bottom+ 5, UIScreenWidth - 20, 0.5)];
    line.backgroundColor = GH_lineColor;
    [whiteView addSubview:line];
    
    
    self.confimpwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 10 + line.st_bottom, UIScreenWidth - 20, 44)];
    self.confimpwdTextFiled.placeholder = @"确认新密码";
    self.confimpwdTextFiled.keyboardType = UIReturnKeyDefault;
    self.confimpwdTextFiled.textColor= GH_secendTextColor;
    self.confimpwdTextFiled.secureTextEntry = YES;
    self.confimpwdTextFiled.clearButtonMode = UITextFieldViewModeAlways;
    [whiteView addSubview:self.confimpwdTextFiled];
    
    UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(10, self.confimpwdTextFiled.st_bottom+ 5, UIScreenWidth - 20, 1)];
    line2.backgroundColor = GH_lineColor;
    [whiteView addSubview:line2];
    
    whiteView.st_height = line2.st_bottom + 15;
    header.st_height = whiteView.st_bottom;
    self.tableView.tableHeaderView = header;
}
- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"确定"
                                                   titleColor:GH_YellowBackGroundColor
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:GH_BlackBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        if (!weakSelf.oldTextFiled.text.length) {
            [SVProgressHUD showErrorWithStatus:@"请输入原密码"];
            return ;
        }
        if (!weakSelf.pwdTextFiled.text.length) {
            [SVProgressHUD showErrorWithStatus:@"请输入新密码"];
            return ;
        }
        if (!weakSelf.confimpwdTextFiled.text.length) {
            [SVProgressHUD showErrorWithStatus:@"请再次输入密码"];
            return ;
        }
        if (![weakSelf.confimpwdTextFiled.text isEqualToString:weakSelf.pwdTextFiled.text]) {
            [SVProgressHUD showErrorWithStatus:@"两次密码输入不一致"];
            return ;
        }
        [self sendChangePwdRequest];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 0    ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 11;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 1;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendChangePwdRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    if (self.oldTextFiled.text.length) {
         [dic setObject:self.oldTextFiled.text forKey:@"old_password"];
    }
    if (self.pwdTextFiled.text.length) {
        [dic setObject:self.pwdTextFiled.text forKey:@"password"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/system/set_login_pass"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD showSuccessWithStatus:@"更改成功"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

